<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改' " :close-on-click-modal='false'
		:close-on-press-escape='false' :fullscreen='true'>
		<el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" @keyup.enter.native="dataFormSubmitHandle()"
			label-width="120px">
			<el-row>
				<el-col :span="4">
					<el-form-item label="出库单号" prop="orderkey">
						<el-input v-model="dataForm.orderkey" placeholder="出库单号" disabled></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="4">
					<el-form-item label="明细行号" prop="line">
						<el-input v-model="dataForm.line" placeholder="明细行号" readonly :disabled="dataForm.status!=0"
							@click="openOrderdetailDialogHandle()"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="4">
					<el-form-item v-if="!!dataForm.id" label="明细记录行号" prop="linelist">
						<el-input v-model="dataForm.linelist" placeholder="明细记录行号"
							@click="openOrderdetailDialogHandle()" :disabled="dataForm.status!=0"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="4">
					<el-form-item label="状态" prop="status">
						<!-- <el-input v-model="dataForm.status" placeholder="状态"></el-input> -->
						<el-select v-model="dataForm.status" placeholder="请选择" disabled>
							<el-option v-for="item in ordersStatus" :key="item.list" :label="item.listname"
								:value="reString(item.list)">
							</el-option>
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="4">
					<el-form-item label="物料编码" prop="sku">
						<el-input v-model="dataForm.sku" placeholder="物料编码" readonly :disabled="dataForm.status!=0"
							@click="openOrderdetailDialogHandle()"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="4">
					<el-form-item label="单位" prop="unit">
						<el-input v-model="dataForm.unit" placeholder="单位" readonly :disabled="dataForm.status!=0">
						</el-input>
					</el-form-item>
				</el-col>
				<el-col :span="4">
					<el-form-item label="包装" prop="pack">
						<el-input v-model="dataForm.pack" placeholder="包装" readonly :disabled="dataForm.status!=0">
						</el-input>
					</el-form-item>
				</el-col>
				<el-col :span="4">
					<el-form-item label="预计数量" prop="preqty">
						<el-input v-model="preqty" placeholder="预计数量" disabled></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="4">
					<el-form-item label="已分配量" prop="allocationqty">
						<el-input v-model="allocationqty" placeholder="已分配量" disabled></el-input>
					</el-form-item>
				</el-col>

				<!-- <el-col :span="4">
					<el-form-item label="实际数量" prop="actqty">
						<el-input v-model="dataForm.actqty" placeholder="实际数量"></el-input>
					</el-form-item>
				</el-col> -->
			</el-row>
			<el-row>
				<!-- <el-col :span="4">
					<el-form-item label="自库位" prop="fromloc">
						<el-input v-model="dataForm.fromloc" placeholder="自库位"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="4">
					<el-form-item label="自托盘" prop="fromtray">
						<el-input v-model="dataForm.fromtray" placeholder="自托盘"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="4">
					<el-form-item label="自容器" prop="frombox">
						<el-input v-model="dataForm.frombox" placeholder="自容器"></el-input>
					</el-form-item>
				</el-col> -->
				<el-col :span="4">
					<el-form-item label="批号" prop="lot">
						<el-input v-model="dataForm.lot" placeholder="批号" @click="openInventoryDialogHandle()" readonly
							:disabled="dataForm.status!=0"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="4">
					<el-form-item label="当前库位" prop="loc">
						<el-input v-model="dataForm.loc" placeholder="当前库位" @click="openInventoryDialogHandle()"
							readonly :disabled="dataForm.status!=0"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="4">
					<el-form-item label="当前箱" prop="tray">
						<el-input v-model="dataForm.tray" placeholder="当前箱" @click="openInventoryDialogHandle()"
							readonly :disabled="dataForm.status!=0"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="4">
					<el-form-item label="当前格" prop="box">
						<el-input v-model="dataForm.box" placeholder="当前格" @click="openInventoryDialogHandle()"
							readonly :disabled="dataForm.status!=0"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="4">
					<el-form-item label="数量" prop="qty">
						<el-input v-model="dataForm.qty" placeholder="数量"></el-input>
					</el-form-item>
				</el-col>
				<!-- <el-col :span="4">
					<el-form-item label="至库位" prop="toloc">
						<el-input v-model="dataForm.toloc" placeholder="至库位"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="4">
					<el-form-item label="至托盘" prop="totray">
						<el-input v-model="dataForm.totray" placeholder="至托盘"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="4">
					<el-form-item label="至容器" prop="tobox">
						<el-input v-model="dataForm.tobox" placeholder="至容器"></el-input>
					</el-form-item>
				</el-col> -->
			</el-row>
			<el-row>

				<el-col :span="4">
					<el-form-item label="生产日期" prop="lot1">
						<el-input v-model="dataForm.lot1" placeholder="生产日期" disabled></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="4">
					<el-form-item label="有效日期" prop="lot2">
						<el-input v-model="dataForm.lot2" placeholder="有效日期" disabled></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="4">
					<el-form-item label="是否加长" prop="lot3">
						<el-input v-model="dataForm.lot3" placeholder="是否加长" disabled></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="4">
					<el-form-item label="价格" prop="lot4">
						<el-input v-model="dataForm.lot4" placeholder="价格" disabled></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="4">
					<el-form-item label="部门/供应商" prop="lot5">
						<el-input v-model="dataForm.lot5" placeholder="部门/供应商" disabled></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="4">
					<el-form-item label="计划单号/工序号" prop="lot6">
						<el-input v-model="dataForm.lot6" placeholder="计划单号/工序号" disabled></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="4">
					<el-form-item label="主制车间" prop="lot7">
						<el-input v-model="dataForm.lot7" placeholder="主制车间" disabled></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="4">
					<el-form-item label="任务编码" prop="lot8">
						<el-input v-model="dataForm.lot8" placeholder="任务编码" disabled></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="4">
					<el-form-item label="新旧" prop="lot9">
						<el-input v-model="dataForm.lot9" placeholder="新旧" disabled></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="4">
					<el-form-item label="作废" prop="lot10">
						<el-input v-model="dataForm.lot10" placeholder="作废" disabled></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="4">
					<el-form-item label="炉批号" prop="lot11">
						<el-input v-model="dataForm.lot11" placeholder="炉批号" disabled></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="4">
					<el-form-item label="材料复验号" prop="lot12">
						<el-input v-model="dataForm.lot12" placeholder="材料复验号" disabled></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="4">
					<el-form-item label="牌号" prop="lot13">
						<el-input v-model="dataForm.lot13" placeholder="牌号" disabled></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="4">
					<el-form-item label="属性14" prop="lot14">
						<el-input v-model="dataForm.lot14" placeholder="属性14" disabled></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="4">
					<el-form-item label="属性15" prop="lot15">
						<el-input v-model="dataForm.lot15" placeholder="属性15" disabled></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="4">
					<el-form-item label="备注" prop="remark">
						<el-input v-model="dataForm.remark" placeholder="备注"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="4">
					<el-form-item label="回传状态" prop="backstatus">
						<el-input v-model="dataForm.backstatus" placeholder="回传状态" disabled></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="4">
					<el-form-item label="回传时间" prop="backdate">
						<el-input v-model="dataForm.backdate" placeholder="回传时间" disabled></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="4">
					<el-form-item label="回传结果" prop="backresult">
						<el-input v-model="dataForm.backresult" placeholder="回传结果" disabled></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="4">
					<el-form-item label="回传码" prop="backcode">
						<el-input v-model="dataForm.backcode" placeholder="回传码" disabled></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="4">
					<el-form-item label="仓库" prop="warehouse">
						<el-input v-model="dataForm.warehouse" placeholder="仓库" disabled></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="4">
					<el-form-item label="拣货人" prop="pickuser">
						<el-input v-model="dataForm.pickuser" placeholder="拣货人" disabled></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="4">
					<el-form-item label="拣货时间" prop="picktime">
						<el-input v-model="dataForm.picktime" placeholder="拣货时间" disabled></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="4">
					<el-form-item label="复核人" prop="checkuser">
						<el-input v-model="dataForm.checkuser" placeholder="复核人" disabled></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="4">
					<el-form-item label="复核时间" prop="checktime">
						<el-input v-model="dataForm.checktime" placeholder="复核时间" disabled></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="4">
					<el-form-item label="装车人" prop="loader">
						<el-input v-model="dataForm.loader" placeholder="装车人" disabled></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="4">
					<el-form-item label="装车时间" prop="loadtime">
						<el-input v-model="dataForm.loadtime" placeholder="装车时间" disabled></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="4">
					<el-form-item label="创建人" prop="adduser">
						<el-input v-model="dataForm.adduser" placeholder="创建人" disabled></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="4">
					<el-form-item label="创建时间" prop="addtime">
						<el-input v-model="dataForm.addtime" placeholder="创建时间" disabled></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="4">
					<el-form-item label="修改人" prop="updatuser">
						<el-input v-model="dataForm.updatuser" placeholder="修改人" disabled></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="4">
					<el-form-item label="修改时间" prop="updatetime">
						<el-input v-model="dataForm.updatetime" placeholder="修改时间" disabled></el-input>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
		</template>

		<OrderdetaliDialog v-if="dialogVisible" ref="orderdetaliDialogVue" @fillline="fillLine"></OrderdetaliDialog>
		<InventoryDialog v-if="dialogInventoryVisible" ref="InventoryDialogVue"
			@filllineInventory="fillLinebyInventory">
		</InventoryDialog>
	</el-dialog>
</template>

<script>
	import {
		ref,
		reactive,
		onMounted,
		nextTick,
		computed
	} from 'vue';
	import {
		getOrderdetaillistById,
		putOrderdetaillist,
		postOrderdetaillist
	} from "@/network/lswms/orders/Orderdetaillist"
	import {
		ElMessage
	} from 'element-plus'
	import {
		getCodeListByCode
	} from "@/network/lswms/base/Codelist.js"

	import OrderdetaliDialog from "@/components/lswms/orders/OrderdetailDialog.vue"
	import InventoryDialog from "@/components/lswms/stock/InventoryDialog1"
	import _ from 'lodash'


	export default {
		name: "orderdetaillistAddOrUpdate",
		emits: ["refreshDataList"],
		components: {
			OrderdetaliDialog,
			InventoryDialog
		},
		setup(props, {
			emit
		}) {
			const visible = ref(false);


			const dataForm = ref({
				id: '',
				orderkey: '',
				line: '',
				linelist: '',
				sku: '',
				unit: '',
				pack: '',
				status: 0,
				qty: '',
				actqty: '',
				fromloc: '',
				fromtray: '',
				frombox: '',
				loc: '',
				tray: '',
				box: '',
				toloc: '',
				totray: '',
				tobox: '',
				lot: '',
				lot1: '',
				lot2: '',
				lot3: '',
				lot4: '',
				lot5: '',
				lot6: '',
				lot7: '',
				lot8: '',
				lot9: '',
				lot10: '',
				lot11: '',
				lot12: '',
				lot13: '',
				lot14: '',
				lot15: '',
				remark: '',
				backstatus: '',
				backdate: '',
				backresult: '',
				backcode: '',
				warehouse: '',
				pickuser: '',
				picktime: '',
				checkuser: '',
				checktime: '',
				loader: '',
				loadtime: '',
				adduser: '',
				addtime: '',
				updatuser: '',
				updatetime: ''
			});


			const dataRule = reactive({
				orderkey: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				line: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				// linelist: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				sku: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				// unit: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				// pack: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				status: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				qty: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				// actqty: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				// fromloc: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				// fromtray: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				// frombox: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				// loc: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				// tray: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				// box: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				// toloc: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				// totray: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				// tobox: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				// lot: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				// lot1: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				// lot2: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				// lot3: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				// lot4: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				// lot5: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				// lot6: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				// lot7: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				// lot8: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				// lot9: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				// lot10: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				// remark: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				// backstatus: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				// backdate: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				// backresult: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				// backcode: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				// warehouse: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				// pickuser: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				// picktime: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				// checkuser: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				// checktime: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				// loader: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				// loadtime: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				// adduser: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				// addtime: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				// updatuser: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				// updatetime: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }]
			})

			const dataFormRef = ref(null)

			const init = () => {
				visible.value = true;
				nextTick(function() {
					dataFormRef.value.resetFields();
					ordersStatusFn();

					if (dataForm.value.id) {
						getorderdetaillistInfo();
					}

				})
			}

			const getorderdetaillistInfo = () => {
				getOrderdetaillistById(dataForm.value.id).then(function(res) {
					if (res.code !== 0) {
						return ElMessage.error(res.msg);
					}
					dataForm.value = _.assignIn({}, dataForm.value, res.data);
				}).catch((err) => {});
			}

			const ordersStatus = ref("");
			const ordersStatusFn = () => {

				getCodeListByCode('OrderStatus').then((res) => {
					if (res.code !== 0) {
						return ElMessage.error(res.msg);
					}
					ordersStatus.value = res.data;
				}).catch((err) => {});
			}
			const dialogVisible = ref(false);
			const orderdetaliDialogVue = ref(null);
			const openOrderdetailDialogHandle = () => {
				dialogVisible.value = true;
				nextTick(function() {
					orderdetaliDialogVue.value.dataForm.orderkey = dataForm.value.orderkey;
					orderdetaliDialogVue.value.init();
				})
			}
			const preqty = ref(0);
			const allocationqty = ref(0);

			const fillLine = (orderdetailRow) => {
				dataForm.value.line = orderdetailRow.line;
				dataForm.value.sku = orderdetailRow.sku;
				preqty.value = orderdetailRow.qty;
				allocationqty.value = orderdetailRow.allocateqty + orderdetailRow.pickqty + orderdetailRow.actqty;
				dataForm.value.pack = orderdetailRow.pack;
				dataForm.value.unit = orderdetailRow.unit;
			}


			const dialogInventoryVisible = ref(false);
			const InventoryDialogVue = ref(null);
			const openInventoryDialogHandle = () => {
				if (dataForm.value.line == "") {
					return ElMessage.error("请先选择出库单明细");
				}


				dialogInventoryVisible.value = true;
				nextTick(function() {
					InventoryDialogVue.value.dataForm.sku = dataForm.value.sku;
					InventoryDialogVue.value.init();
				})
			}

			const inventoryQty = ref(0);

			const fillLinebyInventory = (inventoryRow) => {
				dataForm.value.lot = inventoryRow.lot;
				dataForm.value.loc = inventoryRow.location;
				dataForm.value.tray = inventoryRow.tray;
				dataForm.value.box = inventoryRow.box;
				dataForm.value.qty = inventoryRow.qty;
			}
			// 表单提交
			const dataFormSubmitHandle = _.debounce(() => {
				dataFormRef.value.validate(function(valid) {
					if (!valid) {
						return false;
					}
					if (inventoryQty < dataForm.value.qty) {
						return ElMessage.error("超出库存量，请重新填写");
					}
					let qing = () => {
						return !dataForm.value.id ? postOrderdetaillist(dataForm.value) :
							putOrderdetaillist(dataForm.value)
					}
					qing().then(function(res) {
						if (res.code !== 0) {
							return ElMessage.error(res.msg);
						}
						ElMessage({
							message: '操作成功',
							type: 'success',
							duration: 500,
							onClose: function() {
								visible.value = false
								emit('refreshDataList')
							}
						})
					}).catch(function() {});

				})
			}, 1000, {
				'leading': true,
				'trailing': false
			})


			return {
				visible,
				dataForm,
				dataRule,
				init,
				dataFormSubmitHandle,
				dataFormRef,
				ordersStatus,
				dialogVisible,
				orderdetaliDialogVue,
				openOrderdetailDialogHandle,
				preqty,
				allocationqty,
				fillLine,
				dialogInventoryVisible,
				InventoryDialogVue,
				openInventoryDialogHandle,
				fillLinebyInventory

			}
		},
		methods: {
			//hasPermission
			reString: function(val) {
				let n = Number(val)
				if (!isNaN(n)) {
					return n
				} else {
					return val
				}
			}
		}


	}
</script>

<style scoped>

</style>
